package com.tb.shoop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tb.v1.api.shoop.ISpManagerService;
import com.tb.v1.api.vo.SpManagerVo;
import com.tb.v1.common.exception.CommonException;
import com.tb.v1.entity.SpManager;
import com.tb.v1.result.ResultJson;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: tangBin
 * @Date: 2020/7/14 0014 17:33
 * @Description:
 */
@RequestMapping("/v1")
@RestController
public class SpManagerController extends BaseController {

    @Reference
    private ISpManagerService spManagerService;

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 用户登陆
     * @Date 2020/7/20 0020 14:46
     * @Param [vo]
     **/
    @RequestMapping("/manager")
    public ResultJson<Object> getManager(@RequestBody SpManagerVo vo) {
        //log.info("name={0}==,pwd={1}",name,pwd);
        Map map = new HashMap();
        map.put("username", vo.getName());
        map.put("password", vo.getPwd());
        SpManager user = spManagerService.getManager(map);
        return toResultModel("200", "成功", user);
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 用户列表
     * @Date 2020/7/20 0020 14:15
     * @Param [vo]
     **/
    @RequestMapping("/managerList")
    public ResultJson<Object> getManagerList(SpManagerVo vo) {
        //log.info("name={0}==,pwd={1}",name,pwd);
        Map map = new HashMap();
        Integer page = (vo.getPageIndex() - 1) * vo.getPageSize();
        map.put("page", page);
        map.put("size", vo.getPageSize());
        map.put("name", vo.getQuery());
        List<Map> user = spManagerService.getManagerList(map);
        Long totalNum = spManagerService.getManagerListTotal(map);
        Map resultMap = new HashMap();
        resultMap.put("total", totalNum);
        resultMap.put("pageIndex", vo.getPageIndex());
        resultMap.put("users", user);
        return toResultModel("200", "成功", resultMap);
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 用户列表 更新用户状态
     * @Date 2020/7/20 0020 14:15
     * @Param [uId, type]
     **/
    @PutMapping("/user/{uId}/state/{type}")
    public ResultJson<Object> updateUserState(@PathVariable("uId") String uId, @PathVariable("type") String type) {
        SpManager spManager = new SpManager();
        spManager.setMgState(Integer.valueOf(type));
        spManager.setMgId(Integer.valueOf(uId));
        Integer num = spManagerService.updateEntity(spManager);
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("500", "失败");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 用户列表添加用户信息
     * @Date 2020/7/20 0020 14:15
     * @Param [vo]
     **/
    @PostMapping("/addUser")
    public ResultJson<Object> addUser(@RequestBody SpManagerVo vo) {
        SpManager spManager = new SpManager();
        spManager.setMgName(vo.getName());
        spManager.setMgPwd(vo.getPwd());
        spManager.setMgEmail(vo.getEmail());
        spManager.setMgMobile(vo.getMobile());
        spManager.setMgTime(Long.valueOf(System.currentTimeMillis() / 1000));
        Integer num = spManagerService.saveEntity(spManager);
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("500", "失败");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 用户列表 根据用户id获取用户信息
     * @Date 2020/7/20 0020 14:14
     * @Param [mgId]
     **/
    @GetMapping("/user/{mgId}")
    public ResultJson<Object> getUserById(@PathVariable("mgId") String mgId) {
        SpManager spManager = spManagerService.getEntityById(Long.valueOf(mgId));
        return toResultModel("200", "成功", spManager);

    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 用户列表更新用户
     * @Date 2020/7/20 0020 14:14
     * @Param [mgId, mgEmail, mgMobile]
     **/
    @PutMapping("/user/{mgId}")
    public ResultJson<Object> getUpdateUser(@PathVariable("mgId") String mgId,
                                            @RequestParam String mgEmail,
                                            @RequestParam String mgMobile) throws Exception {
        if (StringUtils.isEmpty(mgEmail) || StringUtils.isEmpty(mgMobile)) {
            throw new CommonException("500", "参数为空");
        }
        SpManager spManager = new SpManager();
        spManager.setMgId(Integer.valueOf(mgId));
        spManager.setMgEmail(mgEmail);
        spManager.setMgMobile(mgMobile);
        Integer num = spManagerService.updateEntity(spManager);
        if (num > 0) {
            return toResultModel("200", "修改成功");
        } else {
            return toResultModel("500", "修改失败");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 用户列表 删除用户
     * @Date 2020/7/20 0020 14:14
     * @Param [mgId]
     **/
    @DeleteMapping("/delete/{mgId}")
    public ResultJson<Object> getDeleteUser(@PathVariable("mgId") String mgId) throws Exception {
        if (StringUtils.isEmpty(mgId)) {
            throw new CommonException("500", "参数为空");
        }
        Integer num = spManagerService.deleteEntity(Long.valueOf(mgId));
        if (num > 0) {
            return toResultModel("200", "删除成功");
        } else {
            return toResultModel("500", "删除失败");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 根据用户分配角色
     * @Date 2020/7/20 0020 14:49
     * @Param [mgId, roleId]
     **/
    @RequestMapping("/update/{mgId}/role")
    public ResultJson<Object> getUpdateUserRole(@PathVariable("mgId") String mgId,
                                                @RequestParam("roleId") String roleId) throws Exception {
        if (StringUtils.isEmpty(roleId) || StringUtils.isEmpty(mgId)) {
            throw new CommonException("500", "参数为空");
        }
        SpManager manager = new SpManager();
        manager.setMgId(Integer.valueOf(mgId));
        manager.setRoleId(Integer.valueOf(roleId));
        Integer num = spManagerService.updateEntity(manager);
        if (num > 0) {
            return toResultModel("200", "修改成功");
        } else {
            return toResultModel("500", "修改失败");
        }
    }
}
